<?php
/**
 * 会员前台品牌管理
 */
defined('IN_PHPCMS') or exit('No permission resources.');
class index{
	private $db;
	public $siteid;
	public $memberinfo;
	
	/**
	 * 我的品牌
	 * Enter description here ...
	 */
	function __construct() {
		$this->db = pc_base::load_model('brand_model');
		$this->siteid = get_siteid();
		
		//获取当前用户信息
		$memberModel = pc_base::load_model('member_model');
		$userid = param::get_cookie('_userid');
		if (!$userid) {
			showmessage("您的会话已过期，请重新登录!", "?m=content");
		}
		//验证用户，获取用户信息
		$this->memberinfo = $memberModel->get_one(array('userid'=>$userid));
		//获取用户模型信息
		$memberModel->set_model($this->memberinfo['modelid']);

		$this->_member_modelinfo = $memberModel->get_one(array('userid'=>$userid));
		$this->_member_modelinfo = $this->_member_modelinfo ? $this->_member_modelinfo : array();
		$memberModel->set_model();
		if(is_array($this->memberinfo)) {
			$this->memberinfo = array_merge($this->memberinfo, $this->_member_modelinfo);
		}
	}
	
	/**
	 * 品牌列表
	 */
	public function init()
	{
		$in = new_addslashes(array_merge($_GET, $_POST));
		
		$siteid = get_siteid();
		$CATEGORYS = getcache('category_content_'.$siteid,'commons');
		
		$total = $this->db->count(array('parentid'=>0, 'userid'=>$this->memberinfo['userid']));
		$brands = $this->db->select(array('userid'=>$this->memberinfo['userid']), 'id,name,parentid,top_catname', '', 'sort DESC, parentid ASC');
		
		$memberinfo = $this->memberinfo;
		$SEO['title'] = '我的品牌 - 会员中心';
		include template('brand', 'init');
	}
	
	/**
	 * 添加品牌
	 */
	public function add()
	{
		$memberinfo = $this->memberinfo;
		$catModel = pc_base::load_model('category_model');
		
		if ($memberinfo['doupload'] != 1) {
			die('抱歉，您暂时没有权限执行添加操作，请联系管理员开启！');
		}
		if ($_POST['dosubmit'] == 1) {
			//判断供应商品牌数量 最多添加6个品牌
			if ($this->db->count(array('parentid'=>0, 'userid'=>$this->memberinfo['userid'])) > 5) {
				showmessage('最多添加6个品牌！');
			}
			
			$catid_1 = intval($_POST['info']['catid_1']);
			$catid_2 = intval($_POST['info']['catid_2']);
			$cat_1 = $catModel->get_one(array('catid'=>$catid_1), 'catid,catname');
			$cat_2 = $catModel->get_one(array('catid'=>$catid_2), 'catid,catname');
			$data = array(
				'userid' => $memberinfo['userid'],
				'username'	=> $memberinfo['username'],
				'catid'		=> $catid_2,
				'top_catname'	=> $cat_1['catname'].' > '.$cat_2['catname'],
				'name'			=> $_POST['info']['name'],
				'parentid'		=> 0,
				'create_time'	=> SYS_TIME,
				'pcompanyname'	=> $_POST['info']['pcompanyname'] ? $_POST['info']['pcompanyname']:$memberinfo['companyname'],
				'prealname'	=> $_POST['info']['prealname'] ? $_POST['info']['prealname']:$memberinfo['realname'],
				'pqq'	=> $_POST['info']['pqq'] ? $_POST['info']['pqq']:$memberinfo['qq'],
				'ptel'	=> $_POST['info']['ptel'] ? $_POST['info']['ptel']:$memberinfo['tel'],
				'paddress'	=> $_POST['info']['paddress'] ? $_POST['info']['paddress']:$memberinfo['address'],
			);
			
			if ($bid = $this->db->insert($data, true)) {
				$this->db->update(array('sort'=>$bid), 'id='.$bid);
				//添加子类
				if (!empty($_POST['info']['child'])) {
					$_POST['info']['child'] = str_replace('，', ',', $_POST['info']['child']);
					$childs = explode(',', $_POST['info']['child']);
					foreach ($childs as $v) {
						$data['parentid'] = $bid;
						$data['name'] = trim($v);
						$data['sort'] = $bid;
						$this->db->insert($data);
					}
				}
				showmessage('操作成功！');
			} else {
				showmessage('添加失败！');
			}
		}

		//栏目
		$cats = array();
		$first_cats = $catModel->select(array('parentid' => TOP_PRODUCT_CATID, 'ismenu'=>1), 'catid, catname');
		foreach ($first_cats AS $v) {
			$v['child'] = $catModel->select(array('parentid' => $v['catid']), 'catid, catname');
			if ($v['child']) {
				$cats[] = $v;
			}
		}
		$cats_json = json_encode($cats);
		
		include template('brand', 'add');
	}
	
	/**
	 * 添加分类
	 */
	public function addClass()
	{
		$memberinfo = $this->memberinfo;
		if (!intval($_GET['id'])) {
			showmessage('品牌不存在');
		}
		
		$brand = $this->db->get_one(array('id'=>intval($_GET['id'])));
		if ($brand['parentid'] != 0) {
			showmessage('数据错误！');
		}
		
		if ($memberinfo['userid'] != $brand['userid']) {
			showmessage('您没有权限对该品牌进行添加分类操作！');
		}
		if (isset($_POST['info']['name'])) {
			$catModel = pc_base::load_model('category_model');
			$data = array(
				'userid' => $brand['userid'],
				'username'	=> $brand['username'],
				'catid'		=> $brand['catid'],
				'top_catname'	=> $brand['top_catname'],
				'name'			=> $_POST['info']['name'],
				'parentid'		=> $brand['id'],
				'sort'			=> $brand['id'],
				'create_time'	=> SYS_TIME
			);
			
			if ($this->db->insert($data)) {
				showmessage('操作成功！');
			}else {
				showmessage('添加失败！');
			}
		}
		
		
		include template('brand', 'addClass');
	}
	
	/**
	 * 编辑品牌
	 * 
	 */
	public function edit()
	{
		$bid = intval($_GET['id']);
		$brand = $this->db->get_one('`id`='.$bid);
		$memberinfo = $this->memberinfo;
		$catModel = pc_base::load_model('category_model');
		
		if ($_POST['dosubmit']) {
			$update_data = array(
				'name'		=>	$_POST['info']['name'],
			);
			//栏目
			/*if ($_POST['info']['catid_2'] != $brand['catid']) {
				$cat_2 = $catModel->get_one('catid='.$_POST['info']['catid_2'], 'catid,catname,parentid');
				$cat_1 = $catModel->get_one('catid='.$cat_2['parentid'], 'catid,catname,parentid');
				$update_data['catid'] = $cat_2['catid'];
				$update_data['top_catname'] = $cat_1['catname'].' > '.$cat_2['catname'];
			}*/
			//会员
			if ($brand['userid'] != $_POST['info']['userid']) {
				$update_data['pcompanyname'] = ($_POST['info']['pcompanyname'] ? $_POST['info']['pcompanyname']:$memberinfo['companyname']);
				$update_data['prealname']	= ($_POST['info']['prealname'] ? $_POST['info']['prealname']:$memberinfo['realname']);
				$update_data['pqq']	= ($_POST['info']['pqq'] ? $_POST['info']['pqq']:$memberinfo['qq']);
				$update_data['ptel'] = ($_POST['info']['ptel'] ? $_POST['info']['ptel']:$memberinfo['tel']);
				$update_data['paddress'] = ($_POST['info']['paddress'] ? $_POST['info']['paddress']:$memberinfo['address']);
			}
			$this->db->update($update_data, array('id'=>$bid));
			//更新子分类
			unset($update_data['name']);
			$this->db->update($update_data, array('parentid'=>$bid));
			
			//更新关联产品信息 
			/*if (isset($cat_2['catid']) && $cat_2['catid']>0) {
				$this->db->updateRelativeProduct($brand['id'], $cat_2['catid']);
			}*/
			
			showmessage('操作成功！');
		}
		//栏目
		$cats = array();
		$first_cats = $catModel->select(array('parentid' => TOP_PRODUCT_CATID, 'ismenu'=>1), 'catid, catname');
		foreach ($first_cats AS $v) {
			$v['child'] = $catModel->select(array('parentid' => $v['catid']), 'catid, catname');
			if ($v['child']) {
				$cats[] = $v;
			}
		}
		$cats_json = json_encode($cats);
		
		$cat_2 = $catModel->get_one('catid='.$brand['catid'], 'catid,catname,parentid');
		$cat_1 = $catModel->get_one('catid='.$cat_2['parentid'], 'catid,catname,parentid');
		include template('brand', 'edit');
	}
	
	/**
	 * 更新分类
	 */
	public function editClass()
	{
		$bid = intval($_GET['id']);
		$brand = $this->db->get_one('`id`='.$bid);

		if ($_POST['dosubmit'] == 1) {
			$this->db->update(array('name'=>$_POST['info']['name']), '`id`='.$bid);
			showmessage('操作成功！');
		}

		include template('brand', 'editClass');
	}
	
	/**
	 * 删除品牌
	 */
	public function delete()
	{
		$bid = intval($_GET['id']);
		if ($bid > 0) {
			$brands = $this->db->select('`id`='.$bid.' OR `parentid`='.$bid, 'id');
			$ids = $bid;
			foreach ($brands AS $v) {
				$ids .= ','.$v['id'];
			}
			if (!empty($ids)) {
				$content_model = pc_base::load_model('content_model');
				$content_model->set_model(13);  //只针对产品模型
				$content_model->update(array('bids'=>""), '`bids` IN('.$ids.')');
			}
			
			$this->db->delete("`id`=".$bid." or `parentid`=".$bid);
		}
		
		header('Location:'.APP_PATH.'index.php?m=brand');
	}
	
	
	/**
	 * 栏目转换成品牌
	 */
	public function change()
	{
		set_time_limit(0);
		
		$default_userid = 39;
		$default_username = '108wo@163.com';
		$top_catid = TOP_PRODUCT_CATID;
		
		$cat_db = pc_base::load_model('category_model');
		$cat_priv_db = pc_base::load_model('category_priv_model');
		
		$content_db = pc_base::load_model('content_model');
		$content_db->set_model(13);
		
		$brand_db = pc_base::load_model('brand_model');
		$hits_db = pc_base::load_model('hits_model');
		$tags_content_db = pc_base::load_model('tags_content_model');
		$attachment_db = pc_base::load_model('attachment_model');
		$attachment_index_db = pc_base::load_model('attachment_index_model');
		
		$user_db = pc_base::load_model('member_model');
		//$user_db->set_model();   //10供应商  12设计师
		
		$level1_cats = $cat_db->select('parentid='.$top_catid, 'catid,catname');
		foreach ($level1_cats AS $l1) {
			$level1_catid = $l1['catid'];
			$level1_catname = $l1['catname'];
			$level2_cats = $cat_db->select('parentid='.$level1_catid, 'catid,catname');
			
			foreach ($level2_cats AS $l2) {
				//获取3级栏目
				$level3_cats = $cat_db->select('parentid='.$l2['catid'], 'catid,userid,catname');
				if ($level3_cats) {
					foreach ($level3_cats AS $l3) {
						//创建品牌
						if (!$l3['userid']) {
							$l3['userid'] = $default_userid;
						}
						$level3_user = $user_db->get_one(array('userid'=>$l3['userid']), 'userid,username,modelid');
						$user_db->set_model($level3_user['modelid']);
						$level3_user = array_merge($level3_user, $user_db->get_one(array('userid'=>$l3['userid'])));
						$user_db->set_model("");
						
						$level1_brand = array(
							'userid'	=> ($level3_user['userid'] ? $level3_user['userid'] : ''),
							'username'	=> ($level3_user['username'] ? $level3_user['username'] : ''),
							'catid'		=> $l2['catid'],
							'top_catname'	=> $level1_catname.' > '.$l2['catname'],
							'name'		=>	$l3['catname'],
							'parentid'	=>	0,
							'sort'		=>	0,
							'pcompanyname'	=>	$level3_user['companyname'],
							'prealname'		=>	$level3_user['realname'],
							'pqq'			=>	$level3_user['qq'],
							'paddress'		=>	$level3_user['address'],
							'ptel'			=>	$level3_user['tel'],
						);
						$level1_bid = $this->insertBrand($level1_brand);
						
						$level4_cats = $cat_db->select('parentid='.$l3['catid'], 'catid,userid,catname');
						if ($level4_cats) {
							//存在4级栏目
							foreach ($level4_cats AS $l4) {
								//创建分类
								$level2_brand = $level1_brand;
								$level2_brand['name'] = $l4['catname'];
								$level2_brand['parentid'] = $level1_bid;
								$level2_brand['sort'] = $level1_id;
								
								$level2_bid = $this->insertBrand($level2_brand);
								
								//更新内容
								$content_list = $content_db->select('catid='.$l4['catid'], 'id,catid');
								if ($content_list) {
									foreach ($content_list AS $c) {
										$content_db->update(array('catid'=>$l2['catid'], 'bids'=>$level2_bid), 'id='.$c['id']);
										$hits_db->update(array('catid'=>$l2['catid']), 'hitsid="c-13-'.$c['id'].'"');
										$tags_content_db->update(array('catid'=>$l2['catid'], 'contentid='.$c['id']));
										
										//附件关联表
										$att = $attachment_index_db->select(array('keyid'=>'c-'.$c['catid'].'-'.$c['id']), 'keyid,aid');
										if ($att) {
											foreach ($att AS $at) {
												$attachment_index_db->update(array('keyid'=>'c-'.$l2['catid'].'-'.$c['id']), 'keyid="'.$at['keyid'].'"');
											}
										}
									}
								}
								
								//更新附件表
								$attachment_db->update(array('catid'=>$l2['catid']), 'catid='.$l4['catid']);
								
								//删除栏目
								$cat_db->delete(array('catid'=>$l4['catid']));
								$cat_priv_db->delete(array('catid'=>$l4['catid']));
							}
						} else {
							//没有4级栏目
							//更新内容
							$content_list = $content_db->select('catid='.$l3['catid'], 'id,catid');
							if ($content_list) {
								foreach ($content_list AS $c) {
									$content_db->update(array('catid'=>$l2['catid'], 'bids'=>$level1_bid), 'id='.$c['id']);
									$hits_db->update(array('catid'=>$l2['catid']), 'hitsid="c-13-'.$c['id'].'"');
									$tags_content_db->update(array('catid'=>$l2['catid'], 'contentid='.$c['id']));
									
									//附件关联表
									$att = $attachment_index_db->select(array('keyid'=>'c-'.$c['catid'].'-'.$c['id']), 'keyid,aid');
									if ($att) {
										foreach ($att AS $at) {
											$attachment_index_db->update(array('keyid'=>'c-'.$l2['catid'].'-'.$c['id']), 'keyid="'.$at['keyid'].'"');
										}
									}
								}
							}
						}
						
						//更新附件表
						$attachment_db->update(array('catid'=>$l2['catid']), 'catid='.$l3['catid']);
						
						//删除栏目
						$cat_db->delete(array('catid'=>$l3['catid']));
						$cat_priv_db->delete(array('catid'=>$l3['catid']));
						
						unset($level3_user);
					}
				}
			}
		}
		die('over!');
	}
	
	/**
	 * 添加品牌
	 */
	private function insertBrand($data)
	{
		$brand_db = pc_base::load_model('brand_model');
		$data['create_time'] = time();
		
		$insert_id = $brand_db->insert($data, true);
		if ($data['sort'] == 0) {
			$brand_db->update(array('sort'=>$insert_id), 'id='.$insert_id);
		}
		return $insert_id;
	}
	
	/**
	 * 转换清单中的商品
	 */
	public function changeOrderDetail()
	{
		set_time_limit(0);
		$cat_db = pc_base::load_model('category_model');
		$cat_priv_db = pc_base::load_model('category_priv_model');
		
		$content_db = pc_base::load_model('content_model');
		$content_db->set_model(13);
		
		$brand_db = pc_base::load_model('brand_model');
		
		$order_detail_db = pc_base::load_model('order_detail_model');
		
		$order_details = $order_detail_db->select('','id,productid');
		foreach ($order_details AS $od) {
			$product = $content_db->get_one(array('id'=>$od['productid']), 'id,catid,title,bids');
			if ($product) {
				$catid = $product['catid'];
				if ($product['bids']) {
					$brand = $brand_db->get_one(array('id'=>$product['bids']));
					if ($brand) {
						if ($brand['parentid']==0) {
							$bid = $brand['id'];
						} else {
							$bid = $brand['parentid'];
						}
						
						$update_data = array(
							'catid' 		=> $catid,
							'bid'			=> $bid,
							'supplierid'	=> $brand['userid'],
							'suppliername'	=> $brand['username'],
							'companyname'	=> $brand['pcompanyname'],
							'nickname'		=> $brand['prealname'],
							'qq'			=> $brand['pqq'],
							'tel'			=> $brand['ptel'],
						);
						$order_detail_db->update($update_data, 'id='.$od['id']);
					}
				}
			}
			
		}
		
		
		die('OVER');
	}
}
